package com.singzi.javaSenior.threads.buyMilkTea;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Main {
    private static final ReentrantLock queueLock = new ReentrantLock(); //可重入互斥锁
    private static final ReentrantReadWriteLock orderLock = new ReentrantReadWriteLock(); //可重入读写锁

    public static void main(String[] args) {
        buyMilkTea();
//        handleOrder();
    }

    private static void handleOrder() {
        Main main = new Main();
        Thread boss = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        main.addOrder();
                        long waitingTime = (long)(Math.random()*1000);
                        Thread.sleep(waitingTime);

                    }catch (InterruptedException e){
                        System.out.println(e.getMessage());
                    }
                }


            }
        });
        boss.start();
        int EMPLOYEE_CNT = 10;
        Thread []employee = new Thread[EMPLOYEE_CNT];
        for (int i = 0; i < EMPLOYEE_CNT; i++) {
            employee[i] = new Thread(() -> {
                while (true){
                    try {
                        main.viewOrder();
                        long waitingTime = (long)(Math.random()*1000);
                        Thread.sleep(waitingTime);

                    }catch (InterruptedException e){
                        System.out.println(e.getMessage());
                    }
                }


            });
            employee[i].start();


        }

    }

    private void addOrder() throws InterruptedException {
        orderLock.writeLock().lock();//写锁
        long writingTime = (long) (Math.random() * 1000);
        Thread.sleep(writingTime);
        System.out.println("老板新加一笔订单");
        orderLock.writeLock().unlock();
    }
    public void viewOrder() throws InterruptedException {
        orderLock.readLock().lock();//读锁

        long readingTime = (long) (Math.random() * 500);
        Thread.sleep(readingTime);
        System.out.println(Thread.currentThread().getName() + ": 查看订单本");
        orderLock.readLock().unlock();

    }

    private static void buyMilkTea() {
        Main main = new Main();
        int STUDENT_CNT = 10;
        Thread[]students = new Thread[STUDENT_CNT];
        for (int i = 0; i < STUDENT_CNT; i++) {
            students[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep((long)(Math.random()*1000));
                        main.tryBuyMilkTea();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            });
            students[i].start();
        }


    }
    public void tryBuyMilkTea() throws InterruptedException {
        boolean flag = true;
        while (flag){
            if(queueLock.tryLock()){//判断是否上锁
                long thinkingTime = (long)(Math.random()*500);
                Thread.sleep(thinkingTime);
                System.out.println(Thread.currentThread().getName() + ": 来一杯奶茶，不加糖");
                flag = false;
                queueLock.unlock();
            }else {
                System.out.println(Thread.currentThread().getName() + ": 再等等");
            }
            if(flag){
                Thread.sleep(1000);
            }
        }
    }

}
